﻿
Partial Class WebForm_ManageMeeting_PLVSkuType
    Inherits System.Web.UI.Page
    Dim cmanager As New ChannelManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.QtyOrGross = Me.GetParameter("QtyOrGross")
            Me.Month = Me.GetParameter("Month")
            Me.SkuType = Me.GetParameter("SkuType")
            Me.lblPLVSkuType.Text = Me.SkuType
            Me.LevelName = Me.GetParameter("LevelName")
            Me.LevelValue = Me.GetParameter("LevelValue")
            Me.BindData()
        End If
    End Sub

    Private Function GetParameter(ByVal ParameterName As String) As String
        If Request.QueryString(ParameterName) IsNot Nothing Then
            Return Request.QueryString.Item(ParameterName).ToString()
        Else
            Return ""
        End If
    End Function

    Private Sub BindData()
        Dim SumString, FilterStringA, FilterStringB As String
        Dim PriceValue As String = 1
        If Me.QtyOrGross = "Gross" Then
            PriceValue = String.Format("STD_Mon{0}", Me.Month)
        End If

        Dim dt As DataTable = Me.Session(Me.SessionID & "ProductLevel")
        Dim dtRows As DataRow() = dt.Select(String.Format("LevelName='{0}'", LevelName))

        If dtRows.Length > 0 Then
            'If dtRows(0)("Chain").ToString() = "Y" Then
            '    FilterStringA = String.Format("substring(Code,1,{0}*2) = '{1}' ", dtRows(0)("Sequence").ToString(), Me.LevelValue)
            'Else
            '    FilterStringA = String.Format("substring(Code,{0}*2-1,2) = '{1}' ", dtRows(0)("Sequence").ToString(), Me.LevelValue)
            'End If
            FilterStringA = String.Format("substring(Code,{0}*2-1,2) = '{1}' ", dtRows(0)("Sequence").ToString(), Me.LevelValue)

            Select Case Me.SkuType
                Case "Sample"
                    FilterStringA &= "and MaterialType in('S2','S3') "
                Case "Tester"
                    FilterStringA &= "and MaterialType='T2' "
                Case "Trial"
                    FilterStringA &= "and MaterialType='T1' "
                Case "Others"
                    FilterStringA &= "and MaterialType not in('S2','S3','T2','T1') "
            End Select

            SumString = String.Format("Mon{0}*{1}", Me.Month, PriceValue)

            'PM
            FilterStringB = "and SkuType='PM' "
            Me.lblPMValue.Text = "=" & Me.GetData(SumString, FilterStringA & FilterStringB)

            'VIP
            FilterStringB = "and SkuType='VIP' "
            Me.lblVIPValue.Text = "=" & Me.GetData(SumString, FilterStringA & FilterStringB)

            'TRADE
            FilterStringB = "and SkuType='TRADE' "
            Me.lblTRADEValue.Text = "=" & Me.GetData(SumString, FilterStringA & FilterStringB)

            'PBOM
            SumString = String.Format("PBOM_Mon{0}*{1}", Me.Month, PriceValue)
            FilterStringB = "and SkuType in('PM','VIP','TRADE') "
            Me.lblPBOMValue.Text = "=" & Me.GetData(SumString, FilterStringA & FilterStringB)
        End If
    End Sub

    Private Function GetData(ByVal SumString As String, ByVal Filterstring As String) As Decimal
        Me.PLVReportData.Columns("ExpressColumn").Expression = SumString
        Dim tmpV As Object = Me.PLVReportData.Compute("Sum(ExpressColumn)", Filterstring)

        If tmpV IsNot DBNull.Value Then
            If Me.QtyOrGross = "Qty" Then
                Return Math.Round(tmpV, 0, MidpointRounding.AwayFromZero)
            Else
                Return Math.Round(tmpV / 1000, 0, MidpointRounding.AwayFromZero)
            End If
        Else
            Return 0
        End If
    End Function

#Region "屬性"
    Protected Property QtyOrGross() As String
        Set(ByVal value As String)
            If value.IndexOf("Quantity") <> -1 Then
                ViewState("QtyOrGross") = "Qty"
            Else
                ViewState("QtyOrGross") = "Gross"
            End If
        End Set
        Get
            Return ViewState("QtyOrGross")
        End Get
    End Property

    Protected Property Month() As String
        Set(ByVal value As String)
            ViewState("Month") = value
        End Set
        Get
            Return ViewState("Month")
        End Get
    End Property

    Protected Property SkuType() As String
        Set(ByVal value As String)
            ViewState("SkuType") = value
        End Set
        Get
            Return ViewState("SkuType")
        End Get
    End Property

    Protected Property LevelName() As String
        Set(ByVal value As String)
            ViewState("LevelName") = value
        End Set
        Get
            Return ViewState("LevelName")
        End Get
    End Property

    Protected Property LevelValue() As String
        Set(ByVal value As String)
            If value.IndexOf(")") <> -1 Then
                ViewState("LevelValue") = value.Substring(1, value.IndexOf(")") - 1)
            Else
                ViewState("LevelValue") = value
            End If
        End Set
        Get
            Return ViewState("LevelValue")
        End Get
    End Property

    Protected ReadOnly Property PLVReportData() As DataTable
        Get
            Return Session(Me.SessionID & "PLVReportData")
        End Get
    End Property

    Public ReadOnly Property SessionID() As String
        Get
            Return Me.Request.QueryString.Item("SessionID")
        End Get
    End Property
#End Region

End Class
